Function getStdOut(oShell,cmd)
	Set oExec = oShell.Exec(cmd)
	Do While oExec.Status = 0
		WScript.Sleep 100
	Loop
	getStdOut = ""
	If Not oExec.StdOut.AtEndOfStream Then
		getStdOut = oExec.StdOut.ReadLine
	End If
End Function

set oShell = CreateObject ("WSCript.shell")
set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "[^a-zA-Z0-9-]+"
VMName = UCase(myRegExp.Replace(getStdOut(oShell,"C:\Program Files\Citrix\XenTools\xenstore_client.exe read name"),""))
OHName = UCase(myRegExp.Replace(getStdOut(oShell,"hostname"),""))
if (Len(VMName)>0 AND Len(OHName)>0 AND StrComp(VMName,OHName)) then
	'if msgbox("The hostname will be changed from '" & OHName & "' to '" & VMName & "'",1)=1 then 
	'oShell.Run("msg * /TIME:10 /W " & "The hostname will be changed from '" & OHName & "' to '" & VMName & "'",5,true)
	'Result = oShell.Run("msg * /TIME:10 /W " & OHName & "->" & VMName, 5, true)
	' Prepare registry keys
	sCCS = "HKLM\SYSTEM\CurrentControlSet\"
	sTcpipParamsRegPath = sCCS & "Services\Tcpip\Parameters\"
	sCompNameRegPath = sCCS & "Control\ComputerName\"
	' Modify regestry and shutdown
	with oShell
	.RegDelete sTcpipParamsRegPath & "Hostname"
	.RegDelete sTcpipParamsRegPath & "NV Hostname"
	.RegWrite sCompNameRegPath & "ComputerName\ComputerName", VMName
	.RegWrite sCompNameRegPath & "ActiveComputerName\ComputerName", VMName
	.RegWrite sTcpipParamsRegPath & "Hostname", VMName
	.RegWrite sTcpipParamsRegPath & "NV Hostname", VMName
	'.Run "shutdown -s -f -t 00", 0, false
	'.Run "tsshutdn 0 /POWERDOWN /DELAY:0", 0, false
	end with
	do
		oShell.exec("shutdown -s -f -t 00") 
		WScript.Sleep 1000
	loop
	'end if
end if


